package manage.utils;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

public class BCryptUtil {
    /**
     *  加密密码
     * @param password 用户注册输入的密码 String
     * @return String 加密后的密文
     */
    public static String encryptPassword(String password) {
        BCryptPasswordEncoder bCryptPassword = new BCryptPasswordEncoder();
        return bCryptPassword.encode(password);
    }

    /**
     * 验证密码是否正确
     * @param rawPassword 用户登录输入的密码
     * @param encodedPassword 数据库中存储的密文
     * @return boolean
     */
    public static boolean matches(String rawPassword, String encodedPassword) {
        BCryptPasswordEncoder bCryptPassword = new BCryptPasswordEncoder();
        return bCryptPassword.matches(rawPassword, encodedPassword);
    }

    public static void main(String[] args) {
        String encrypted = encryptPassword("123456");
        String encrypted2 = encryptPassword("admin");
        System.out.println(encrypted);
        System.out.println(encrypted2);
        System.out.println(encrypted2.length());

        System.out.println(matches("123456", encrypted));


    }
}
